/***
  gui_cliente.java is part of Marioneta.

  Marioneta is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License.

  Marioneta is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
  
  @author martin
   Martin Edmundo Barriga Orozco, 2008
   martin.barriga@gulmore.org
  
  gui_cliente: Se encarga de abrir un puerto en el cual va a escuchar
  las peticiones de los clientes, una vez iniciado, lanza cada thread como
  solicitudes tenga para atender a los clientes.
 */

package escritorioremoto;
import java.io.*;
import java.awt.*;

/**
 *
 * @author martin
 */
public class gui_cliente extends Panel {
    cliente boom;
    Frame frmCliente;
    Graphics g;
    int x, y;
    String ip_cliente;
    
    public gui_cliente(String ip ){
        ip_cliente = ip;
        boom = new cliente(ip_cliente, 5000);
        try{
            System.out.println( boom.recibe() );
            String id = boom.recibe();
            boom.send("Mi id es "+ id);
            
            //carga_x();
            
            
            op();
        }
        catch(IOException e){
            System.out.println("Error con el protocolo: "+ e.getMessage() );
        }
    }
    
    public void paint(Graphics g) {
	//g.drawString("Laura", 20, 20);
        //g.drawString("TQM", 100, 100);
    }
    
    public void update(Graphics g) {
        g.drawOval(x, y, 2, 2);
    }
    
    private void carga_x(){
        frmCliente = new Frame("Ventana del cliente");
        frmCliente.setSize(400, 300);
        
        frmCliente.add(this);
        frmCliente.setVisible(true);
        
    }
    
    private void op(){
        try{
            Robot robot = new Robot();
            while(true){
                String str = boom.recibe();
                
                String str1 = str.substring(1, str.indexOf(",") );
                String str2 = str.substring( str.indexOf(",") + 1 );
                x = Integer.parseInt(str1)-5;
                y = Integer.parseInt(str2)-23;
                
                robot.mouseMove(x, y);
                //this.repaint();
                //g.drawOval(Integer.parseInt(str1), Integer.parseInt(str2), 2, 2);
                               
                //robot.mouseMove(Integer.parseInt(str1) , Integer.parseInt(str2));
            }
        }
        catch(AWTException e){
            System.out.println("Robot fail: " + e.getMessage()+ " :(");
        }
        catch(IOException e){
            System.out.println("Error al recibir info" + e.getMessage());
        }
        
    }
        
    public static void main(String[] args) {
        
        
    }
    
}
